package com.study;

/**
 * TableSqlTransformToDiagramTest
 *
 * @author wanghuilong
 * @version 1.0
 * @description
 * @date 2023/4/27 11:16
 */
public class TableSqlTransformToDiagramTest {


    public static void main(String[] args) {
        String tableSql = "CREATE TABLE `mall_dynamic_header` (\n" +
                "  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',\n" +
                "  `user_id` bigint(20) NOT NULL COMMENT '用户ID',\n" +
                "  `data` varchar(2048) DEFAULT NULL COMMENT '数据',\n" +
                "  `remark` varchar(1000) DEFAULT NULL COMMENT '备注',\n" +
                "  `opt_id` bigint(20) DEFAULT NULL COMMENT '操作者id',\n" +
                "  `opt_name` varchar(64) DEFAULT NULL COMMENT '操作者名称',\n" +
                "  `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
                "  `gmt_modify` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',\n" +
                "  `type` varchar(150) NOT NULL DEFAULT '' COMMENT '业务',\n" +
                "  PRIMARY KEY (`id`),\n" +
                "  UNIQUE KEY `idx_type_uid` (`type`,`user_id`)\n" +
                ") ENGINE=InnoDB AUTO_INCREMENT=9345 DEFAULT CHARSET=utf8mb4 COMMENT='动态表头信息'";

        String[] split = tableSql.split("\n");
        String tableEnglishName = split[0].trim().replace("CREATE TABLE `","").replace("` (","");
        System.out.println(tableEnglishName);
        String lastLine = split[split.length - 1].trim();
        String tableChinessName = lastLine.substring(lastLine.indexOf("COMMENT='")+"COMMENT='".length(), lastLine.lastIndexOf("'"));
        System.out.println(tableChinessName);
        System.out.println();
        System.out.println();
        System.out.println(tableEnglishName+":"+tableChinessName);
        System.out.println();
        System.out.println();
        for (int i = 1; i < split.length-1; i++) {
            String line = split[i].trim();
            if(!line.startsWith("`")) {
                continue;
            }
            line = line.replace("`","");
            String comment = null;
            if(line.contains("COMMENT")) {
                comment = line.substring(line.indexOf("COMMENT '")+"COMMENT '".length(), line.lastIndexOf("'"));
            }

            String[] split1 = line.split(" ");
            String fieldEnglishName = split1[0];
            String fieldDataType = split1[1];
            System.out.println(fieldEnglishName+"\t"+fieldDataType+"\t"+comment);
            System.out.println();
        }
    }


}
